﻿
using Dapper;
using MyMES.Domain.yjb;
using MyMES.Infrastructure.Interface;
using NPOI.POIFS.FileSystem;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static Dapper.SqlMapper;


namespace MyMES.Infrastructure.Implement
{

    /// <summary>
    /// 实现仓储接口
    /// </summary>
    public class BaseRepository<T> : IBaseRepository<T> where T : class
    {
        public DatabaseContext db;
        public BaseRepository(DatabaseContext db)
        {
            this.db = db;
        }

        /// <summary>
        /// 获取所有实体
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<T> GetByIdAsync(int id)
        {
            using (IDbConnection conn = db.Connection)
            {
                conn.Open();
                var tableName = typeof(T).Name;
                return await conn.QueryFirstOrDefaultAsync<T>(
                    $"SELECT * FROM {tableName} WHERE {tableName}Id = @id",
                    new { id }
                );
            }
        }



        /// <summary>
        /// 根据id获取列表
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="selectSql"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<List<T>> GetList(int Id, string selectSql)
        {

            using (IDbConnection conn = db.Connection)
            {
                conn.Open();
                // 在新的任务中执行查询操作并将结果转换为列表
                return await Task.Run(() => conn.Query<T>(selectSql, new { Id = Id }).ToList());
            }
        }

        /// <summary>
        /// 获取指定 ID 的单个实体
        /// </summary>
        /// <param name="Id"></param>
        /// <param name="selectOneSql"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<T> GetOne(int Id, string selectOneSql)
        {
            using (IDbConnection conn = db.Connection)
            {
                conn.Open();
                // 在新的任务中执行查询操作并将结果转换为单个实体
                return  await conn.QueryFirstOrDefaultAsync<T>(selectOneSql, new { Id = Id });
            }
        }

        /// <summary>
        /// 插入实体
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="insertSql"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Insert(T entity, string insertSql)
        {
            using (IDbConnection conn = db.Connection)
            {
                conn.Open();
                // 在新的任务中执行查询操作并将结果转换为单个实体
               await conn.QueryFirstOrDefaultAsync<T>(insertSql, entity);
            }
        }

        public void Insert(Approval model)
        {
            throw new NotImplementedException();
        }



        /// <summary>
        /// 插入实体-----批量插入
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="insertSql"></param>
        /// <returns></returns>
        public async Task Insertobject(object entity, string insertSql)
        {
            using (IDbConnection conn = db.Connection)
            {
                conn.Open();
                // 在新的任务中执行查询操作并将结果转换为单个实体
                await conn.QueryFirstOrDefaultAsync<T>(insertSql, entity);
            }
        }

        /// <summary>
        /// 通用查询实现（核心改进点）
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="sql"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        public async Task<IEnumerable<T>> QueryAsync<T>(string sql, object param = null)
        {
            using (IDbConnection conn = db.Connection)
            {
                 conn.Open();
                return await conn.QueryAsync<T>(sql, param);
            }
        }








        /// <summary>
        /// 根据sql语句查询
        /// </summary>
        /// <param name="selectSql"></param>
        /// <returns></returns>
        public async Task<List<T>> SelectAsync(string selectSql)
        {
            using (IDbConnection conn = db.Connection)
            {
                conn.Open();
                // 在新的任务中执行查询操作并将结果转换为单个实体
                return await Task.Run(() => conn.Query<T>(selectSql).ToList());
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="updateSql"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Update(T entity, string updateSql)
        {
            using (IDbConnection conn = db.Connection)
            {
                conn.Open();
                await conn.ExecuteAsync(updateSql, entity);
            }
        }


        /// <summary>
        /// 状态修改
        /// </summary>
        /// <param name="entity"></param>
        /// <param name="updateSql"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task Updatestatus(object entity, string updateSql)
        {
            using (IDbConnection conn = db.Connection)
            {
                conn.Open();
                await conn.ExecuteAsync(updateSql, entity);
            }
        }
    }
}
